<?php
/**
 * Ecma-262 5rd 7.4
 * 
 * Comment并非Token的一种，但是这里为了实现的方便，将其抽象为一种token
 *
 */

class Comment extends Token {
    
    const MultiLineComment = 1;
    const SingleLineComment = 2;
    
    public $commentType;
    
    public function getCommentType() {
        return $this->commentType;
    }
    
    public function setCommentType($commentType) {
        $this->commentType = $commentType;
    }
    
    public function __construct() {
        parent::__construct ();
        $this->tokenType = Token::COMMENT;
    }
    
    /**
     * 
     * 获取单行注释
     * 
     * SingleLineComment ::
     *     //SingleLineCommentChars opt
     * 
     * SingleLineCommentChars ::
     *     SingleLineCommentChar SingleLineCommentChars opt
     *
     * SingleLineCommentChar ::
     *     SourceCharacter but not LineTerminator
     * 
     * @param string $stream
     * @param int $pos
     */
    public static function getSingleLineComment(&$stream,$pos){
        $char1 = $stream[$pos+0];
        $char2 = $stream[$pos+1];
        
        $singleLineCommentChars = '';
        
        if($char1=='/' && $char2=='/'){
            for ($i = $pos+2;;$i++){
                $tempChar = $stream[$i];
                if(LineTerminator::isLineTerminator($tempChar)){
                    break;
                }
                
                $singleLineCommentChars .= $tempChar;
            }
            
            return "//".$singleLineCommentChars;
        }
        
        return false;
    }
    
    
    // 获取多行注释
    // MultiLineComment ::
    //     /* MultiLineCommentChars opt */
    // MultiLineCommentChars ::
    //     MultiLineNotAsteriskChar MultiLineCommentChars opt 
    //     * PostAsteriskCommentChars opt
    // PostAsteriskCommentChars ::
    //     MultiLineNotForwardSlashOrAsteriskChar MultiLineCommentChars opt
    //     * PostAsteriskCommentChars opt
    // MultiLineNotAsteriskChar ::
    //     SourceCharacter but not asterisk *
    // MultiLineNotForwardSlashOrAsteriskChar ::
    //     SourceCharacter but not forward-slash / or asterisk *
    public static function getMultiLineComment(&$stream,$pos){
        // 放在LexicalAbalyzer::_getToken中实现了
    }
}